package cn.edu.jxau.test;

import java.util.Arrays;
import java.util.Objects;

import cn.edu.jxau.util.LinkedStack;
import cn.edu.jxau.util.Stack;

public class Main {

	public static void main(String[] args) {

		int[] arr = new int[] { 0, 1, 2, 3, 4, 5, 6, 7 };
		System.out.println(foo(arr));
	}

	public static boolean foo(int[] arr) {

		Objects.requireNonNull(arr, "参数arr不能为null");
		if (arr.length != 8) {
			throw new IllegalArgumentException("参数arr的长度不为8");
		}
		return permutation(arr, 0);
	}

	private static boolean permutation(int[] arr, int start) {

		if (start >= arr.length) {
			if (sum(arr, 0, 1, 2, 3) == sum(arr, 4, 5, 6, 7)) {
				if (sum(arr, 0, 2, 4, 6) == sum(arr, 1, 3, 5, 7)) {
					if (sum(arr, 0, 1, 4, 5) == sum(arr, 2, 3, 6, 7)) {
						System.out.println(Arrays.toString(arr));
					}
				}
			}
		} else {
			for (int i = start; i < arr.length; i++) {
				swap(arr, start, i);
				permutation(arr, start + 1);
				swap(arr, start, i);
			}
		}
		return false;
	}

	private static void swap(int[] arr, int i, int j) {

		int temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
	}

	private static int sum(int[] arr, int a, int b, int c, int d) {
		return arr[a] + arr[b] + arr[c] + arr[d];
	}
}